/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import ar.com.greensoft.neosystem.dao.GenericDao;
import ar.com.greensoft.neosystem.dao.LocalidadDao;
import ar.com.greensoft.neosystem.dao.ObraSocialDao;
import ar.com.greensoft.neosystem.dao.ProvinciaDao;
import ar.com.greensoft.neosystem.model.ObraSocial;
import ar.com.greensoft.neosystem.model.address.Localidad;
import ar.com.greensoft.neosystem.model.address.Pais;
import ar.com.greensoft.neosystem.model.address.Provincia;
import ar.com.greensoft.neosystem.model.references.TipoDocumento;
import ar.com.greensoft.neosystem.search.SearchFilter;
import ar.com.greensoft.neosystem.search.clientes.ObraSocialSearchFilter;
import ar.com.greensoft.web.jsf.AbstractFacadeBean;

/**
 * 
 * @author Jorge E. Villaverde
 * @date 05/07/2013
 * @version 1.0.0
 */
@ManagedBean
@ViewScoped
public class ObraSocialBean 
	extends AbstractFacadeBean<ObraSocial> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// Properties ---------------------------------------------------------------------------------

    protected Pais pais;
    
    protected Provincia provincia;

    // Dependencies -------------------------------------------------------------------------------
	
	@EJB
	private ObraSocialDao dao;
    @EJB
    private LocalidadDao localidadDao;
    @EJB
    private ProvinciaDao provinciaDao;
    	
	// Actions ------------------------------------------------------------------------------------
	
	@Override
	protected ObraSocial newEntity() {
		ObraSocial entity = new ObraSocial();
		entity.setTipoDocumento(TipoDocumento.CUIT);
		return entity;
	}

	// Getters/setters ----------------------------------------------------------------------------
	
	@Override
	protected GenericDao<ObraSocial, Long> getDao() {
		return dao;
	}	
	
	public Pais getPais() {
		return pais;
	}

	public void setPais(Pais pais) {
		this.pais = pais;
	}

	public Provincia getProvincia() {
		return provincia;
	}

	public void setProvincia(Provincia provincia) {
		this.provincia = provincia;
	}
	
	public List<Provincia> getProvincias(){
		if(this.pais == null)
			return null;
		return provinciaDao.findByPais(pais);
	}
	
	public List<Localidad> getLocalidades(){
		if(this.provincia == null)
			return null;
		return localidadDao.findByProvincia(provincia);
	}

	@Override
	protected SearchFilter createSearchFilter() {
		ObraSocialSearchFilter filter = new ObraSocialSearchFilter();
		return filter;
	}

	@Override
	protected String getEntityName(ObraSocial entity) {
		return entity.getNombre();
	}

	@Override
	protected ObraSocial findEntityByName(String entityName) {
		return dao.findByName(entityName);
	}	
	
	public void cuitChangeListener(AjaxBehaviorEvent event) {
		String cuit = getEntity().getCuit();
		ObraSocial entity = dao.findByCuit(cuit);

		if(entity != null){
			setEntity(entity);
			setEditing(true);
			if(getEntity().getDireccion().getLocalidad() != null){
				this.provincia = getEntity().getDireccion().getLocalidad().getProvincia();
				this.pais = getEntity().getDireccion().getLocalidad().getProvincia().getPais();
			}
		}else{
			if(isEditing()){
				create();
				setEditing(false);
			}
		}
	}
	
	@Override
	protected void beforeCreate() {
		super.beforeCreate();
		try {
			getEntity().setNumeroDocumento(Long.parseLong(getEntity().getCuit()));
		} catch (NumberFormatException e) {
			logger.error(e.getMessage());
		}
	}
}
